﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
//using Smart.Text;
using Smart.Web.Mobile;

namespace MobileWebMvc.Settings
{
    /// <summary>
    /// 
    /// </summary>
    public class SitePolicy
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Encoding GetEncodingForCurrentTarget()
        {
            Carrier carrier = WebMobileContext.Current.Carrier;

            Encoding encoding = Encoding.UTF8;
            if( WebMobileContext.Current.IsMobile )
            {
                string encode = ( ( ( carrier == Carrier.DoCoMo ) || ( carrier == Carrier.Au ) ) ? "Shift_JIS" : "UTF-8" );
                encoding = WebMobileContext.Current.CarrierSettings.GetMobileEncoding( encode, PictgramTreat.Convert );
            }

            return encoding;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="response"></param>
        public static void SetResponseForCurrentTarget(HttpResponseBase response)
        {
            Carrier carrier = WebMobileContext.Current.Carrier;

            // エンコーディング
            Encoding encoding = GetEncodingForCurrentTarget();

            // レンダリング
            IPictgramRender render = null;
            if( carrier == Carrier.DoCoMo )   { render = new DocomoMixedRender(); }
            if( carrier == Carrier.Au )       { render = new SjisNumberRender(); }
            if( carrier == Carrier.Softbank ) { render = new UnicodeNumberRender(); }

            // 出力設定
            response.ContentEncoding = new MobileOutputEncoding( Encoding.Unicode, encoding );
            response.Filter =
                new MobileOutputStream( response.Filter,
                                        Encoding.Unicode,
                                        encoding,
                                        WebMobileContext.Current.CarrierSettings )
                {
                    Trim = true,
                    //Effect = (x) => KanaConverter.Convert( x, KanaOption.Narrow ),
                    PictgramRender = render,
                };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="response"></param>
        public static void SetContentTypeForCurrentTarget(HttpResponseBase response)
        {
            Carrier carrier = WebMobileContext.Current.Carrier;

            if( carrier == Carrier.DoCoMo )   { response.ContentType = "application/xhtml+xml"; }
            if( carrier == Carrier.Au )       { response.ContentType = "application/xhtml+xml"; }
            if( carrier == Carrier.Softbank ) { response.ContentType = "application/xhtml+xml"; }
        }
    }
}
